﻿using MVCProject.BLL;
using MVCProject.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.UI;
using System.Xml.Linq;

namespace MVCProject.API.Controllers
{
    [ApiAuthorize]
    public class UsersController : ApiController
    {
        // GET: api/Users
        //[ApiActionMonitor]
        /// <summary>
        /// 分页获取用户数据
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">每页条数</param>
        /// <param name="name">模糊查询</param>
        /// <returns></returns>
        public ResultPageList<UsersDto> Get(int page, int limit, string name = "")
        {
            UsersBll bll = new UsersBll();

            int count = 0;
            var list = bll.GetUsersByPage(page, limit, name, out count);

            var data = from u in list
                       select new UsersDto
                       {
                           UserID = u.userID,
                           UserName = u.userName,
                           Sex = u.sex,
                           Birthday = u.birthday,
                           ImgID = u.imgID,
                           Path = u.Images.path,
                           Phone = u.phone,
                           LoginName = u.loginName,
                           LoginPwd = u.loginPwd,
                           Status = u.status,
                           RoleID = u.roleID,
                           RoleName = u.Roles.roleName,
                           DeptID = u.Roles.deptID,
                           DeptName = u.Roles.Department.deptName
                       };

            ResultPageList<UsersDto> result = new ResultPageList<UsersDto>();
            result.code = 0;
            result.msg = "ok";
            result.count = count;
            result.data = data.ToList();

            return result;
        }

        // GET: api/Users/5
        public string Get(int id)
        {
            string name = "zhansg";
            int num = 10;
            return "666";
        }


        // POST: api/Users
        public Result Post([FromBody] UsersFormDto value)
        {
            Users users = new Users();
            users.userID = value.UserID;
            users.userName = value.UserName;
            users.sex = value.Sex;
            users.birthday = value.Birthday;
            users.imgID = value.Sex == "男" ? 3 : 4;
            users.phone = value.Phone;
            users.loginName = value.LoginName;
            users.loginPwd = MD5Helper.Md5(value.LoginPwd, "X2");
            users.status = value.Status;
            users.roleID = value.RoleID;
            users.lastTime = DateTime.Now;

            UsersBll bll = new UsersBll();
            bool bol = bll.Add(users);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "添加成功!" : "添加失败!";

            return result;
        }

        // PUT: api/Users/5
        public Result Put([FromBody] UsersFormDto value)
        {
            UsersBll bll = new UsersBll();

            var user = bll.Query().Where(u => u.userID == value.UserID).FirstOrDefault();

            Result result = new Result();


            if (user != null)
            {
                user.userName = value.UserName;
                user.sex = value.Sex;
                user.birthday = value.Birthday;
                user.imgID = value.Sex == "男" ? 3 : 4;
                user.phone = value.Phone;
                user.loginName = value.LoginName;
                //user.loginPwd = MD5Helper.Md5(value.LoginPwd, "X2");
                user.status = value.Status;
                user.roleID = value.RoleID;
                user.lastTime = DateTime.Now;

                bool bol = bll.Update(user);

                result.code = bol ? 1 : 0;
                result.msg = bol ? "修改成功!" : "修改失败!";


            }
            else
            {
                result.code = -1;
                result.msg =  "未找到数据!";
            }

            return result;



        }

        // DELETE: api/Users/5
        public Result Delete(int id)
        {
            UsersBll bll = new UsersBll();

            Result result = new Result();

            bool bol = bll.Delete(id);
            result.code = bol ? 1 : 0;
            result.msg = bol ? "删除成功!" : "删除失败!";
            return result;
        }

        // DELETE: api/Users/5
        [Route("api/Users/batch")]
        public Result DeleteBatch(int[] ids)
        {
            UsersBll bll = new UsersBll();
            Result result = new Result();
            bool bol = bll.Delete(ids);
            result.code = bol ? 1 : 0;
            result.msg = bol ? "删除成功!" : "删除失败!";
            return result;
        }
    }
}
